import requests
from loguru import logger

from basic.base_login import TOKEN
from config import ENV

def request_hook(response, *args, **kwargs):
    logger.debug(f"接口响应结果：{response.text}")

class BaseReqest:
    def __init__(self):
        self._session = requests.Session()
        self._session.headers.update({"Content-Type": "application/json", "token": TOKEN})
        self._session.hooks["response"].append(request_hook)
        self._requests = self._session

    def _post(self, api_name, url, data):
        base_url = ENV.HOST + url
        logger.debug(f"接口_{api_name}_请求参数：{base_url}")
        return self._requests.post(f"{base_url}", json=data).json()

    def _get(self, api_name, url, params=None):
        base_url = ENV.HOST + url
        logger.debug(f"接口_{api_name}_请求参数：{base_url}")
        return self._requests.get(f"{base_url}", params=params).json()

    def _put(self, api_name, url, data):
        base_url = ENV.HOST + url
        logger.debug(f"接口_{api_name}_请求参数：{base_url}")
        return self._requests.put(f"{base_url}", json=data).json()

    def _delete(self, api_name, url, params=None):
        base_url = ENV.HOST + url
        logger.debug(f"接口_{api_name}_请求参数：{base_url}")
        return self._requests.delete(f"{base_url}", params=params).json()